#!/bin/bash
. ./lib

options=()
options+=("Config" "/boot/loader/loader.conf")
options+=("Entries" "/boot/loader/entries")
options+=("" "")
options+=("Update" "bootctl update")
options+=("" "")
if [ ! -f /etc/pacman.d/hooks/systemd-boot.hook ];  then
  options+=("Create pacman hook" "/etc/pacman.d/hooks/systemd-boot.hook")
else
  options+=("Delete pacman hook" "/etc/pacman.d/hooks/systemd-boot.hook")
fi

defaultitem="Config"
sel=$(whiptail --backtitle "$apptitle" --title "Systemd Config Menu :" --menu "" --default-item "$defaultitem" --cancel-button "Back" 0 0 0 \
  "${options[@]}" \
  3>&1 1>&2 2>&3)
if [ ! "$?" = "0" ]; then
  exit 1
fi
sed -i "/^defaultitem=/c\defaultitem=\"$sel\"" $0

case $sel in
  'Config')
    defaultitem="Config"
    editfile /boot/loader/loader.conf
  ;;
  'Entries')
    defaultitem="Entries"
    menu config/boot/systemd/entries
  ;;
  'Update')
    defaultitem="Update"
    clear
    bootctl update
    pressanykey
  ;;
  'Create pacman hook')
    mkdir -p /etc/pacman.d/hooks
    cat <<EOF > /etc/pacman.d/hooks/systemd-boot.hook
[Trigger]
Type = Package
Operation = Upgrade
Target = systemd

[Action]
Description = Updating systemd-boot
When = PostTransaction
Exec = /usr/bin/bootctl update    
EOF
  ;;
  'Delete pacman hook')
    rm /etc/pacman.d/hooks/systemd-boot.hook
  ;;
esac

exit 0
